package com.everbest.admin.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;


public class DateUtil {
	public final static DateTimeFormatter DTF_DATE_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
	public final static DateTimeFormatter DTF_DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd");
	public final static DateTimeFormatter DTF_TIME = DateTimeFormatter.ofPattern("HH:mm:ss");
	//默认零点时间
	public final static String TIME_00_00_00 = " 00:00:00";

	public static Date parseDate(String dateStr, String[] pattern) {
		dateStr = dateStr.trim();
		Date date = null;
		try {
			if (pattern == null) {
				pattern = new String[]{
						"yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyyMMddHHmmss","yyyy年MM月dd日HH:mm",
						"yyyy-MM-dd", "yyyy/MM/dd", "yyyyMMdd","yyyy年MM月dd日",
						"yyyy-MM", "yyyy/MM", "yyyyMM"
				};
			}
			date = DateUtils.parseDate(dateStr, pattern);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}
//
//	public static void main(String[] args) {
//		System.out.println(toDateString(parseDate("2019年02月22日", null),"yyyy-MM-dd"));
//	}

	public static String toDateString(Date date, String pattern){
		String str = "";
		try {
			if(StringUtils.isEmpty(pattern)){
				pattern = "yyyy-MM-dd HH:mm:ss";
			}
			SimpleDateFormat sdf =   new SimpleDateFormat( pattern );
			str = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}
}
